﻿using System; 
using System.Collections.Generic;
using System.Text;
using System.Data.SqlClient;
using System.Data;
using Matrix.Common.DBUtility;
using Matrix.MaKaYunShang.Model;

namespace Matrix.MaKaYunShang.DAL
{
	public partial class VariableQrCodeDAL : BaseDAL<VariableQrCodeDAL>
	{
        #region 默认方法
        /// <summary>
        /// 增加一条数据
        /// </summary>
        public int Add(VariableQrCode model)
        {
            var strSql = new StringBuilder();
            strSql.Append("insert into VariableQrCode(");
            strSql.Append("ShopId,QrCodeContent,StartTime,EndTime,RequestSource,RequestUserId,Longitude,Latitude,DeviceId,AddTime,IsDelete");
            strSql.Append(") values (");
            strSql.Append("@ShopId,@QrCodeContent,@StartTime,@EndTime,@RequestSource,@RequestUserId,@Longitude,@Latitude,@DeviceId,@AddTime,@IsDelete");
            strSql.Append(") ");
            strSql.Append(";select @@IDENTITY");
            SqlParameter[] parameters = {
			            new SqlParameter("@ShopId", SqlDbType.Int,4) ,            
                        new SqlParameter("@QrCodeContent", SqlDbType.NVarChar,1024) ,            
                        new SqlParameter("@StartTime", SqlDbType.DateTime) ,            
                        new SqlParameter("@EndTime", SqlDbType.DateTime) ,            
                        new SqlParameter("@RequestSource", SqlDbType.Int,4) ,            
                        new SqlParameter("@RequestUserId", SqlDbType.Int,4) ,            
                        new SqlParameter("@Longitude", SqlDbType.NVarChar,16) ,            
                        new SqlParameter("@Latitude", SqlDbType.NVarChar,16) ,            
                        new SqlParameter("@DeviceId", SqlDbType.Int,4) ,            
                        new SqlParameter("@AddTime", SqlDbType.DateTime) ,            
                        new SqlParameter("@IsDelete", SqlDbType.Bit,1)             
              
            };

            parameters[0].Value = model.ShopId;
            parameters[1].Value = model.QrCodeContent;
            parameters[2].Value = model.StartTime;
            parameters[3].Value = model.EndTime;
            parameters[4].Value = model.RequestSource;
            parameters[5].Value = model.RequestUserId;
            parameters[6].Value = model.Longitude;
            parameters[7].Value = model.Latitude;
            parameters[8].Value = model.DeviceId;
            parameters[9].Value = model.AddTime;
            parameters[10].Value = model.IsDelete; var obj = DbHelperSQL.GetSingle(strSql.ToString(), parameters);
            return obj == null ? 0 : Convert.ToInt32(obj);
        }

        /// <summary>
        /// 更新一条数据
        /// </summary>
        public bool Update(VariableQrCode model)
        {
            var strSql = new StringBuilder();
            strSql.Append("update VariableQrCode set ");

            strSql.Append(" ShopId = @ShopId , ");
            strSql.Append(" QrCodeContent = @QrCodeContent , ");
            strSql.Append(" StartTime = @StartTime , ");
            strSql.Append(" EndTime = @EndTime , ");
            strSql.Append(" RequestSource = @RequestSource , ");
            strSql.Append(" RequestUserId = @RequestUserId , ");
            strSql.Append(" Longitude = @Longitude , ");
            strSql.Append(" Latitude = @Latitude , ");
            strSql.Append(" DeviceId = @DeviceId , ");
            strSql.Append(" AddTime = @AddTime , ");
            strSql.Append(" IsDelete = @IsDelete  ");
            strSql.Append(" where Id=@Id ");

            SqlParameter[] parameters = {
			            new SqlParameter("@Id", SqlDbType.Int,4) ,            
                        new SqlParameter("@ShopId", SqlDbType.Int,4) ,            
                        new SqlParameter("@QrCodeContent", SqlDbType.NVarChar,1024) ,            
                        new SqlParameter("@StartTime", SqlDbType.DateTime) ,            
                        new SqlParameter("@EndTime", SqlDbType.DateTime) ,            
                        new SqlParameter("@RequestSource", SqlDbType.Int,4) ,            
                        new SqlParameter("@RequestUserId", SqlDbType.Int,4) ,            
                        new SqlParameter("@Longitude", SqlDbType.NVarChar,16) ,            
                        new SqlParameter("@Latitude", SqlDbType.NVarChar,16) ,            
                        new SqlParameter("@DeviceId", SqlDbType.Int,4) ,            
                        new SqlParameter("@AddTime", SqlDbType.DateTime) ,            
                        new SqlParameter("@IsDelete", SqlDbType.Bit,1)             
              
            };

            parameters[0].Value = model.Id;
            parameters[1].Value = model.ShopId;
            parameters[2].Value = model.QrCodeContent;
            parameters[3].Value = model.StartTime;
            parameters[4].Value = model.EndTime;
            parameters[5].Value = model.RequestSource;
            parameters[6].Value = model.RequestUserId;
            parameters[7].Value = model.Longitude;
            parameters[8].Value = model.Latitude;
            parameters[9].Value = model.DeviceId;
            parameters[10].Value = model.AddTime;
            parameters[11].Value = model.IsDelete; return DbHelperSQL.ExecuteSql(strSql.ToString(), parameters) > 0;
        }

        /// <summary>
        /// 删除一条数据
        /// </summary>
        public bool Delete(int Id)
        {
            var strSql = new StringBuilder();
            strSql.Append("update VariableQrCode set IsDelete=1 ");
            strSql.Append(" where Id=@Id");
            SqlParameter[] parameters = {
					new SqlParameter("@Id", SqlDbType.Int,4)
			};
            parameters[0].Value = Id;

            return DbHelperSQL.ExecuteSql(strSql.ToString(), parameters) > 0;
        }

        /// <summary>
        /// 批量删除一批数据
        /// </summary>
        public bool DeleteList(string Idlist)
        {
            var strSql = new StringBuilder();
            strSql.Append("update VariableQrCode set IsDelete=1 ");
            strSql.Append(" where Id in (" + Idlist + ")  ");
            return DbHelperSQL.ExecuteSql(strSql.ToString()) > 0;
        }

        /// <summary>
        /// 得到一个对象实体
        /// </summary>
        public DataSet GetModel(int Id)
        {
            var strSql = new StringBuilder();
            strSql.Append("select Id, ShopId, QrCodeContent, StartTime, EndTime, RequestSource, RequestUserId, Longitude, Latitude, DeviceId, AddTime, IsDelete  ");
            strSql.Append("  from VariableQrCode ");
            strSql.Append(" where Id=@Id  and IsDelete=0");
            SqlParameter[] parameters = {
					new SqlParameter("@Id", SqlDbType.Int,4)
			};
            parameters[0].Value = Id;

            var ds = DbHelperSQL.Query(strSql.ToString(), parameters);
            return ds.Tables[0].Rows.Count > 0 ? ds : null;
        }

        /// <summary>
        /// 获得数据列表
        /// </summary>
        public DataSet GetList(string strWhere)
        {
            var strSql = new StringBuilder();
            strSql.Append("select * ");
            strSql.Append(" FROM VariableQrCode where IsDelete=0 ");
            if (!string.IsNullOrEmpty(strWhere))
            {
                strSql.Append(" and " + strWhere);
            }
            return DbHelperSQL.Query(strSql.ToString());
        }
        #endregion

        #region 扩展方法

        /// <summary>
        /// 删除一条数据
        /// </summary>
        public bool DeleteByMerchantId(int merchantId)
        {
            var strSql = new StringBuilder();
            strSql.Append("update VariableQrCode set IsDelete=1 ");
            strSql.Append(" where MerchantId=@Id");
            SqlParameter[] parameters = {
				new SqlParameter("@Id", SqlDbType.Int,4)
			};
            parameters[0].Value = merchantId;
            var rows = DbHelperSQL.ExecuteSql(strSql.ToString(), parameters);
            return rows > 0;
        }

        /// <summary>
        /// 得到一个对象实体
        /// </summary>
        /// <remarks>create lk 2016-06-22</remarks>
        public DataSet GetModelByQrCodeContent(string qrCodeContent)
        {
            var strSql = new StringBuilder();
            strSql.Append("select * from VariableQrCode ");
            strSql.Append(" where QrCodeContent=@QrCodeContent and IsDelete=0");
            SqlParameter[] parameters = {
				new SqlParameter("@QrCodeContent", SqlDbType.VarChar)
			};
            parameters[0].Value = qrCodeContent;
            var ds = DbHelperSQL.Query(strSql.ToString(), parameters);
            return ds.Tables[0].Rows.Count > 0 ? ds : null;
        }
        #endregion
	}
}
